package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.zhgd.util.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zhgd.dto.TensionProcessDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.TensionProcessBo;
import com.ruoyi.zhgd.domain.vo.TensionProcessVo;
import com.ruoyi.zhgd.domain.TensionProcess;
import com.ruoyi.zhgd.mapper.TensionProcessMapper;
import com.ruoyi.zhgd.service.ITensionProcessService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author zyb
 * @date 2023-09-21
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class TensionProcessServiceImpl implements ITensionProcessService {

    private final TensionProcessMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public TensionProcessVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<TensionProcessVo> queryPageList(TensionProcessBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TensionProcess> lqw = buildQueryWrapper(bo);
        Page<TensionProcessVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<TensionProcessVo> queryList(TensionProcessBo bo) {
        LambdaQueryWrapper<TensionProcess> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TensionProcess> buildQueryWrapper(TensionProcessBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TensionProcess> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), TensionProcess::getCode, bo.getCode());
        lqw.eq(StringUtils.isNotBlank(bo.getEquipmentNumber()), TensionProcess::getEquipmentNumber, bo.getEquipmentNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getHoleNumber()), TensionProcess::getHoleNumber, bo.getHoleNumber());
        lqw.eq(bo.getRecordTime() != null, TensionProcess::getRecordTime, bo.getRecordTime());
        lqw.eq(bo.getTensionTime() != null, TensionProcess::getTensionTime, bo.getTensionTime());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus1()), TensionProcess::getStatus1, bo.getStatus1());
        lqw.eq(bo.getTensionForce1() != null, TensionProcess::getTensionForce1, bo.getTensionForce1());
        lqw.eq(bo.getOilPressure1() != null, TensionProcess::getOilPressure1, bo.getOilPressure1());
        lqw.eq(bo.getTopStroke1() != null, TensionProcess::getTopStroke1, bo.getTopStroke1());
        lqw.eq(bo.getElongation1() != null, TensionProcess::getElongation1, bo.getElongation1());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus2()), TensionProcess::getStatus2, bo.getStatus2());
        lqw.eq(bo.getTensionForce2() != null, TensionProcess::getTensionForce2, bo.getTensionForce2());
        lqw.eq(bo.getOilPressure2() != null, TensionProcess::getOilPressure2, bo.getOilPressure2());
        lqw.eq(bo.getTopStroke2() != null, TensionProcess::getTopStroke2, bo.getTopStroke2());
        lqw.eq(bo.getElongation2() != null, TensionProcess::getElongation2, bo.getElongation2());
        lqw.eq(bo.getTotalElongation() != null, TensionProcess::getTotalElongation, bo.getTotalElongation());
        lqw.eq(bo.getDesignElongation() != null, TensionProcess::getDesignElongation, bo.getDesignElongation());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(TensionProcessBo bo) {
        TensionProcess add = BeanUtil.toBean(bo, TensionProcess.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(TensionProcessBo bo) {
        TensionProcess update = BeanUtil.toBean(bo, TensionProcess.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TensionProcess entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public PageUtils tensionProcessList(Page page, Map<String, Object> params) {
        List<Map> tensionProcessList = baseMapper.tensionProcessList(page, params);
        return new PageUtils(page.setRecords(tensionProcessList));
    }

    @Override
    public PageUtils selectHouleNumber(Page page,Map<String, Object> params) {
        List<Map> tensionProcessVos = baseMapper.selectHouleNumber(page,params);
        return new PageUtils(page.setRecords(tensionProcessVos));
    }

    @Override
    public List<TensionProcessDto> holeNumberSjfx(Map<String, Object> params) {
        List<TensionProcessDto> tensionProcessDtos = baseMapper.holeNumberSjfx(params);
        return tensionProcessDtos;
    }
}
